package com.peixunp.dao.impl;

import java.util.List;

import com.peixunp.bean.News;
import com.peixunp.dao.NewsDao;
import com.peixunp.util.Constants;

import finaler.db.AbstractBaseDao;
import finaler.util.Page;
import finaler.util.PageList;
import finaler.util.SQLUtil;

public class NewsDaoImpl extends AbstractBaseDao<News> implements NewsDao {
	
	@Override
	protected Class<News> getEntityClass() {
		return News.class;
	}

	@Override
	protected boolean isDebug() {
		return Constants.ISDEBUG;
	}
	
	@Override
	public PageList getNewsList(Page page) throws Exception {
		StringBuffer whereSql = new StringBuffer();
		if(page.getParameter("newsKey")!=null && page.getParameter("newsKey").toString().equals("")==false){
			whereSql.append(" where newstitle like '%" + page.getParameter("newsKey") +"%'");
			whereSql.append(" or newscontent like '%" + page.getParameter("newsKey") +"%'");
		}
		PageList pageList = new PageList(page);//固定
		StringBuffer sqlCount = new StringBuffer();
		sqlCount.append("select count(1) from news ");
		sqlCount.append( whereSql );
		int dataCount = super.getIntBySql( sqlCount.toString() );//取总
		pageList.setTotal(dataCount);//总数
		
		StringBuffer queryListSql = new StringBuffer();
		queryListSql.append(" select *");
		queryListSql.append(" from news  ");
		queryListSql.append( whereSql );
		queryListSql.append(" order by id desc");
		SQLUtil pageSQL = new SQLUtil(queryListSql);//固定
		pageSQL.setFirstResult( page.getStart() );//固定
		pageSQL.setMaxResults( page.getRange() );//固定
		List<News> dataList = super.findBySqlToBeanList(pageSQL.getSql());//数据集
		pageList.setList(dataList);
		return pageList;
	}
}
